<?php
require_once("../inc/conn.php");
require_once("../inc/session.php");
//接收传递来的值
$name=$_POST["name"];
$depart=$_POST["depart"];
$pass=$_POST["pass"];
$pass1=$_POST["pass1"];
$sex=$_POST["sex"];
$weixin=$_POST["weixin"];
$address=$_POST["address"];
$ck=implode(",",$_POST["ck"]);

//验证合法性
//一次性处理错误信息
$error="";
//验证用户名
if( strlen($name)<6 || strlen($name)>16 ){
	//echo "<script>alert('请输入6-16位字符！');location.href='addinfo.php';</script>";
	$error="请输入6-16位用户名字符！\\n";
}

//通过sql验证用户名
    $ssql="select * from userinfo where name='".$name."'";	
	$rss=$conn->query($ssql);
	$total=$rss->num_rows;
	//判断
	if($total!=0){
		//echo "<script>window.alert('已经被注册！！'); location.href='addinfo.php';</script>";
		$error=$error."用户名已经被注册！\\n";
	}
//验证部门

	if(strlen($depart)==0){
		$error=$error."请选择部门！\\n";
	}
//验证密码
if(strlen($pass)==0){
	//echo "<script>alert('密码不允许为空！！');location.href='addinfo.php';</script>";
	$error=$error."密码不允许为空！\\n";
}

if($pass!=$pass1){
	//echo "<script>alert('两次密码不相符！');location.href='addinfo.php';</script>";
	$error=$error."两次密码不相符！\\n";
}

//判断兴趣爱好
if(strlen($ck)==0){
	//echo "<script>alert('请至少选择一个兴趣爱好！！');location.href='addinfo.php';</script>";
	$error=$error."请至少选择一个兴趣爱好！\\n";
}

if(strlen($error)!=0){
	//一次性展示错误信息
	echo "<script>alert('".$error."');location.href='addinfo.php';</script>";
}else{
	
	//定义SQL语句  实现写入
$sql="insert into userinfo(name,sortid,pass,sex,weixin,address,ck,dt) values('".$name."','".$depart."','".$pass."','".$sex."','".$weixin."','".$address."','".$ck."',now())";
//执行sql语句
$rs=$conn->query($sql,$conn);
if($rs){
	echo "<script>alert('写入成功！');location.href='userlist.php';</script>";
	
}else{
	echo "<script>alert('写入失败！');location.href='addinfo.php';</script>";
	
}
}




?>